// pages/maintItem/maintItem.js
import config from "../../config.js";

const openIdUrl = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemList: [],
    items: {
      value: []
    },
    isNeed: 0,
    stepcar: 0,
    orderId: -1,
    type: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.type != null) {
      this.setData({
        type: options.type
      })
    }
    this.loadData();
    this.data.isNeed = wx.getStorageSync('isNeed');
    this.data.stepcar = wx.getStorageSync('stepcar');
    var orderId = wx.getStorageSync("orderId");
    if (orderId != null) {
      this.setData({
        orderId: orderId,
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  submit: function() {
    var that = this;
    var userId = wx.getStorageSync("userId");
    var houseKeeperId = wx.getStorageSync("houseKeeperId");
    var orderType = wx.getStorageSync("orderType");
    var serviceType = wx.getStorageSync("serviceType");
    var carId = wx.getStorageSync("carId");
    var orderFrom = wx.getStorageSync("orderFrom");
    var addressReturn = wx.getStorageSync("addressReturn");
    var addressPick = wx.getStorageSync("addressPick");
    var addressBy = wx.getStorageSync("addressBy");
    var returnTime = wx.getStorageSync("returnTime");
    var pickTime = wx.getStorageSync("pickTime");
    var tel = wx.getStorageSync("tel");
    var undetermined = wx.getStorageSync("undetermined");
    var unbdetermined = wx.getStorageSync("unbdetermined");
    var locLatitudePick = wx.getStorageSync("locLatitudePick");
    var locLongitudePick = wx.getStorageSync("locLongitudePick");
    var locLatitudeReturn = wx.getStorageSync("locLatitudeReturn");
    var locLongitudeReturn = wx.getStorageSync("locLongitudeReturn");
    var locLatitudeBy = wx.getStorageSync("locLatitudeBy");
    var locLongitudeBy = wx.getStorageSync("locLongitudeReturnBy");

    if (locLatitudeBy == null || locLatitudeBy == '') {
      locLatitudeBy = 0.0;
    }
    if (locLongitudeBy == null || locLongitudeBy == '') {
      locLongitudeBy = 0.0;
    }
    var items = JSON.stringify(this.data.items);

    var licenseFr = wx.getStorageSync("licenseFr");
    var licenseBa = wx.getStorageSync("licenseBa");
    var idenCardA = wx.getStorageSync("idenCardA");
    var idenCardB = wx.getStorageSync("idenCardB");

    var orderId = that.data.orderId;
    if (orderId != null && orderId == '') {
      orderId = -1;
    }

    wx.request({
      url: openIdUrl.maintenanceSubbmit,
      data: {
        tel: tel,
        orderId: orderId == null ? -1 : orderId,
        carId: carId,
        userId: userId,
        pickTime: pickTime,
        orderType: orderType,
        orderFrom: orderFrom,
        returnTime: returnTime,
        serviceType: serviceType,
        addressPick: addressPick,
        isNeedStepCar: this.data.isNeed,
        stepCarId: this.data.stepcar,
        addressReturn: addressReturn,
        houseKeeperId: houseKeeperId,
        locLatitudePick: locLatitudePick,
        locLongitudePick: locLongitudePick,
        locLatitudeReturn: locLatitudeReturn,
        locLongitudeReturn: locLongitudeReturn,
        locLatitudeBy: locLatitudeBy,
        locLongitudeBy: locLongitudeBy,
        items: items,
        licenseFr: licenseFr,
        licenseBa: licenseBa,
        idenCardA: idenCardA,
        idenCardB: idenCardB,
        unbdetermined: unbdetermined,
        undetermined: undetermined,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          wx.setStorageSync("orderId", res.data.data.oid);
          wx.setStorageSync("orderConfirmType", 1);
          var orderId = wx.getStorageSync("orderId");
          if (orderFrom == 1) {
            wx.navigateTo({
              url: "../orderSuccess/success?id=" + orderId,
              complete: function() {
                wx.setStorageSync("orderConfirmType", null);
                wx.setStorageSync("orderId", null);
              }
            })
          } else {
            wx.setStorageSync("orderConfirmType", 1);
            wx.reLaunch({
              url: "../gjIndex/gjorder/gjorder",
            })
          }
          // wx.showModal({
          //   title: "恭喜您",
          //   content: "预约成功，车管家将会在10分钟内通过电话与您沟通详情 ，请保持电话畅通！谢谢！",
          //   showCancel: false,
          //   confirmColor: "#137AF0",
          //   success: function() {

          //   }
          // })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }

      },
      error: function() {
        wx.showToast({
          title: "请求失败,请联系管理员",
          icon: 'none',
          mask: true,
          duration: 2000
        })
      }
    })
  },
  loadData: function() {
    var that = this;
    wx.request({
      url: openIdUrl.maintenanceItems,
      data: {
        start: 0,
        length: 10
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          console.log(res.data.data);
          if (that.data.type != 1) {
            that.setData({
              itemList: res.data.data
            })
          } else {
            var items = wx.getStorageSync("maintainCategoryies");
            var result = res.data.data;
            for (var i = 0; i < result.length; i++) {
              var item = result[i];
              if (item.maintainCategoryItems != null) {
                for (var j = 0; j < item.maintainCategoryItems.length; j++) {
                  var it = item.maintainCategoryItems[j];
                  for (var x = 0; x < items.length; x++) {
                    var selectIt = items[x];
                    if (selectIt.maintainCategoryItem.itemId == it.itemId) {
                      it.isChoose = 1;
                      item.maintainCategoryItems[j] = it;
                      that.data.items.value.push(it.itemId + "");
                      break;
                    }
                  }
                }
              }
              result[i] = item;
            }
          }

          if (result != null) {
            that.setData({
              itemList: result
            })

          }
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }

      },
      error: function() {
        wx.showToast({
          title: "请求失败,请联系管理员",
          icon: 'none',
          mask: true,
          duration: 2000
        })
      }
    })
  },
  checkItemAction: function(e) {
    console.log(e);
    this.setData({
      items: e.detail
    })
  },
  changeKeeper: function() {
    var instance = this;
    wx.navigateTo({
      url: '../managerList/managerList',
    })
  },

})